<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class file_model extends CI_Model{

	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

	function copy_history($source,$target)
	{
		$history = $this->get_file_history_since(0,$source);
	
		$all_data = array();
		
		foreach($history as $single_history)
		{
			$data=array();
			$data['file_id'] = $target;
			$data['operation'] = $single_history->operation;
			$data['updated_time'] = $single_history->updated_time;
			$data['changed_content'] = $single_history->changed_content;
			$data['previous_content'] = $single_history->previous_content;
			$data['changed_object_id'] = $single_history->changed_object_id;
			$data['user_id'] = $single_history->user_id;
			$data['page_id'] = $single_history->page_id;
			array_push($all_data,$data);
		}
		 
		if(!empty($all_data))
			$this->db->insert_batch('file_history', $all_data); 
	}
	function delete($file_id)
	{
		$this->db->delete('file', array('file_id' => $file_id)); 
	}
	function update_history($data)
	{
		$this->db->where('history_id', $data['history_id']);
		$this->db->update('file_history', $data); 
	}
	function get_file_history_since($time,$file_id,$page_id=false)
	{
		$this->db->select('*');
		$this->db->from('file_history');
		$this->db->where('updated_time >',$time);
		$this->db->where('file_id',$file_id);
		if($page_id)
			$this->db->where('page_id',$page_id);
		$query = $this->db->get();
		return $query->result();
	}
	function get_recover_list($time,$file_id,$page_id=false)
	{
		$this->db->select('*');
		$this->db->from('file_history');
		$this->db->where('updated_time >',$time);
		$this->db->where('operation','DEL');
		$this->db->where('file_id',$file_id);
		$this->db->order_by("updated_time", "desc"); 
		if($page_id)
			$this->db->where('page_id',$page_id);
		$query = $this->db->get();
		return $query->result();
	}
	function add_recover_history($data)
	{
		$this->db->insert('recover_deleted_history',$data);
	}
	function is_already_recovered($history_id)
	{
		$this->db->select('*');
		$this->db->from('recover_deleted_history');
		$this->db->where('history_id',$history_id);
		$query = $this->db->get();
		$result = $query->row();
		return sizeof($result)>0;
	}
	function get_history_by_id($history_id){
		$this->db->select('*');
		$this->db->from('file_history');
		$this->db->where('history_id',$history_id);
		$query = $this->db->get();
		return $query->row();
	}
	function get_file_by_file_id($file_id){
		$this->db->select('*');
		$this->db->from('file');
		$this->db->where('file_id',$file_id);
		$query = $this->db->get();
		return $query->row();
	}
	
	function get_chat_history_on_file($file_id){
		$this->db->select('*');
		$this->db->from('chat_history');
		$this->db->join('users', 'users.id = chat_history.user_id');
		$this->db->where('file_id',$file_id);
		$query = $this->db->get();
		return $query->result();
	}
	
	function save($content,$file_id){
	
		if( is_array($content))
		{
			$content['last_update'] = time();
		}else if( is_object($content))
		{
			$content->last_update = time();
		}
		$this->db->where('file_id', $file_id);
		$this->db->update('file', $content); 
	}
	function get_group_by_file_id($file_id){
		//get file data 
		$file_data =  $this->get_file_by_file_id($file_id);
		
		if(empty($file_data))
			return false;
		if($this->config->item('use_infinitec_db', 'ion_auth'))
		{
			$serverName = $this->config->item('awb_manager_ip'); //serverName\instanceName
			
			$connectionInfo = array( "Database"=>$this->config->item('awb_manager_db'), "UID"=>$this->config->item('awb_manager_db_user'), "PWD"=>$this->config->item('awb_manager_db_password'),"CharacterSet"=>"UTF-8");
			$conn = sqlsrv_connect( $serverName, $connectionInfo);
			if(!$conn)
			{
				show_error('Cannot connect to AWB Manager Server');
				return false;
			}
			
			// $sql = "SELECT * FROM AWBSTUDENT WHERE STUDENT_ID = '$user_id' ";
			$sql = "SELECT * FROM AWBGROUP WHERE AWBGROUP_ID = '$file_data->group_id' "; 
			// $sql = "SELECT * FROM STUDENT_M";
			$params = array();

			$stmt = sqlsrv_query( $conn, $sql, $params);
			if( $stmt === false ) {
				 die( print_r( sqlsrv_errors(), true));
			}
			
			$group_data =false;
			while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
				$group_data=$row;
			}	
			$ws_id = $group_data['WS_ID'];
			$sql = "SELECT * FROM WORKSPACE WHERE WS_ID = '$ws_id' "; 
			$params = array();

			$stmt = sqlsrv_query( $conn, $sql, $params);
			if( $stmt ) {
				while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
					$group_data['WS_NAME'] = $row['WS_NAME'];
				}
			}
		}
		else
		{
			$query  =$this->db->query("SELECT * FROM AWBGROUP WHERE AWBGROUP_ID = '$file_data->group_id' "); 
			$group_data =  $query->result();
			if(!empty($group_data))
			{
				$group_data = (array)$group_data[0];
				$ws_id = $group_data['WS_ID'];
				$query  =$this->db->query("SELECT * FROM AWBWORKSPACE WHERE WS_ID = '$ws_id' ");
				$result =  $query->row();
				$result = (array)$result;
				$group_data['WS_NAME'] = $result['WS_NAME'];
			}
		}
		
		return $group_data;
	}
	function save_file_history($data)
	{
		$this->db->insert('file_history',$data);
		return $this->db->insert_id();
	}
	function make_blank_file($group_id,$file_name)
	{
		$blankContent = '{"pages":[{"id":1,"last_update":'.time().',"width":"1450","height":"725","drawing_objects":[]}]}'; //default A4 blank document
		$data = array();
		$data['group_id'] =$group_id ;
		$data['file_name'] =$file_name ;
		$data['content'] =$blankContent ;
		$data['is_template'] =0 ;
		$data['last_update'] =time() ;
		$data['created_on'] =time() ;
		$this->db->insert('file',$data);
		$data['file_id'] =$this->db->insert_id();
		mkdir('documents/'.$data['file_id']);
		
		copy( 'documents/thumb-blank.png' ,'documents/'.$data['file_id'].'/thumb-1.png'  );
		copy( 'documents/background-blank.png' ,'documents/'.$data['file_id'].'/background-1.png'  );
		return $data;
	}
	function save_chat_history($data)
	{
		$this->db->insert('chat_history',$data);
	}
	function delete_page_history($file_id,$page_id)
	{
		$this->db->delete('file_history', array('file_id' => $file_id,'page_id' => $page_id)); 
	}
	function save_file($data)
	{
		$this->db->insert('file',$data);
	}
	
	function get_max_id()
	{
		$query = $this->db->query("SELECT MAX(file_id) AS MAX FROM `file`");
		
		return $query->row();
	}
	
}

?>